package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;

/**
 * @author Sanmier
 * @create 2020-12-04 14:14
 */
@Service
public class ItemServiceImpl implements ItemService {

     //数据汇总
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;


    //获取sku详情信息
    //  http://item.gmall.com/39.html;  web-all 39.html  skuId = 39
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        Map<String, Object> map = new HashMap<>();


    // CompletableFuture

    // Supplier
    CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
        //获取数值
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        map.put("skuInfo", skuInfo);
        return skuInfo;
    },threadPoolExecutor);


    // Consumer
    CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
        //获取分类数据
        BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
        map.put("categoryView", categoryView);
    },threadPoolExecutor);


    //价格
    CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
        BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
        map.put("price", skuPrice);
    },threadPoolExecutor);


    //获取销售属性销售属性值并回显锁定
    CompletableFuture<Void> spuSaleAttrCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo -> {
        List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuInfo.getId(), skuInfo.getSpuId());
        map.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
    }),threadPoolExecutor);


    //获取切换数据
    CompletableFuture<Void> mapCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo -> {
        Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
        //skuValueIdsMap 转换为Json字符串
        String mapJson = JSON.toJSONString(skuValueIdsMap);
        //存储数据
        map.put("valuesSkuJson",mapJson);
    }),threadPoolExecutor);
    //web-all 需要渲染数据 页面需要获取对应的key   key是谁 从页面找  model.addAllAttributes()
    //分类数据的值 = productFeignClient.get分类数据的方法
    // map.put("分类数据的key", "分类数据的值");


    // 调用热度排名
    CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
        Result result = listFeignClient.incrHotScore(skuId);
    },threadPoolExecutor);


    //多任务组合allof
    CompletableFuture.allOf(
            skuInfoCompletableFuture,
            categoryViewCompletableFuture,
            skuPriceCompletableFuture,
            spuSaleAttrCompletableFuture,
            mapCompletableFuture,
            completableFuture
            ).join();
    return map;
    }
}
